package aqie.lambda;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 * clojure
 * BiConsumer
 * BiFunction
 * BinaryOperator
 * Consumer
 *
 * Function
 *
 * Predicate
 * Supplier
 * integers.forEach(Try.of(i -> System.out.println(50 / i)));
 */
public class ListDemo {
    public static void main(String[] args) {
        List<Integer> integers = Arrays.asList(1, 2, 3, 0);
        integers.forEach(i -> System.out.println(Try.of((Integer a)-> a / i).apply(50)));
    }
}
class Try {
    // 将 UncheckedFunction 包装为 Function 的功能
    public static <T, R> Function<T , R> of(UncheckedFunction<T, R> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                return mapper.apply(t);
            } catch (Exception e) {
                System.out.println("catch Exception");
            }
            return null;
        };
    }
    @FunctionalInterface
    public interface UncheckedFunction<T, R> {
        R apply(T t) throws Exception;
    }
}
